import startSearchModel from "@/config/startSearchModel";
import useUserStore from "@/stores/useUserStore";


const startCrudTools = {

    getSearchParams(srcSearchParams, columnsMap) {
        let searchParams = {};
        for (const columnKey in srcSearchParams) {
            let val = srcSearchParams[columnKey];
            if (val === undefined || val === null) continue
            if (typeof val === 'string' && val.length < 1) {
                continue
            }
            let searchModel = columnsMap[columnKey];
            if (!searchModel) {
                continue
            }
            switch (searchModel) {
                case startSearchModel.BETWEEN:
                case startSearchModel.NOT_BETWEEN:
                case startSearchModel.IN:
                case startSearchModel.NOT_IN:
                    searchParams["start_search_" + columnKey] = columnsMap[columnKey] + srcSearchParams[columnKey].join(",")
                    continue;
            }
            searchParams["start_search_" + columnKey] = columnsMap[columnKey] + srcSearchParams[columnKey]
        }
        return searchParams
    },
    hasBtnPermission(vm, option, btnKey, row, index, defaultValue = true) {
        if (!vm.permissionBtn[btnKey]) return defaultValue;
        let code = vm.permissionBtn[btnKey];
        let result;

        if (option.permissionBtnHandler && option.permissionBtnHandler[btnKey]) {

            result = option.permissionBtnHandler[btnKey](vm, row, defaultValue);

        } else {
            result = useUserStore().hasPermission(code, defaultValue)
        }

        return result;
    },
    iteratorColumn(option, filter, consumer) {
        if (!option) {
            return
        }
        if (!consumer) {
            return;
        }
        if (filter == null) {
            filter = function (column, i, columns, option) {
                return true;
            }
        }
        if (!(consumer instanceof Function)) {
            return;
        }
        let columns = option.column || []
        let column;

        for (let i = 0; i < columns.length; i++) {
            column = columns[i]
            if (!filter(column, i, columns, option)) {
                continue
            }

            consumer(column, i, columns, option)
            if (column.type !== 'dynamic') {
                continue
            }
            startCrudTools.iteratorColumn(column.children, filter, consumer)
        }


    },
    findColumns(option, filter) {
        let result = []
        if (filter === null || filter === undefined) {
            console.error("filter cannot be null ")
            return result
        }
        if (typeof filter === 'string') {
            const colProp = filter;
            filter = (column) => {
                return colProp === column.prop;
            }
        }

        let tempResult
        startCrudTools.iteratorColumn(option, (column, i, columns, option) => {
            tempResult = filter(column, i, columns, option);
            console.log(column.prop, tempResult)
            return tempResult
        }, (column, i, columns, option) => {
            result.push(column)
        })
        return result
    }
}
export default startCrudTools
